package commitmsg

import (
	"context"
	"gitee.com/kmyss/gf-ex/yscmd"
	"gitee.com/kmyss/gf-ex/yslog"
	"gitee.com/kmyss/ys-cli/internal/service"
)

type Builder struct {
	yscmd.CommandBuilderCore
}

func (b *Builder) Init(ctx context.Context) {
	b.CommandBuilderCore.Init(ctx)
	b.C.Use = "commit-msg"
	b.C.Short = "检查提交信息符合语义化表示..."
	b.C.Long = `
    为 commit-msg 钩子提供语义化提交信息检查功能. 当提交信息错误时阻止 git commit
`
	b.C.Run = run
	b.C.Help = help
	b.addFlag()

	b.C.Note = service.Git().CommitNoteStr(ctx)
}

func (b *Builder) addFlag() {
	b.SetFlags(&FlagPathBuilder{})
	yscmd.SetDefaultFlag(b)
}

func help(c *yscmd.Command) {
	c.PrintHelp()
}

func run(c *yscmd.Command) {
	err := service.Git().CheckCommitMsg(c.Context(), FlagPath(c.Context()).Get())
	if err != nil {
		yslog.Error(err)
		yslog.Print(service.Git().CommitNoteStr(c.Context()))
		yscmd.CmdWait(c.Context())
		return
	}

	yscmd.TimeToCloseShell(c.Context(), 2)

}

func Cmd(ctx context.Context) *yscmd.Command {
	c := &Builder{}
	c.Init(ctx)
	return c.Command()
}
